package com.yanqu.road.dao.impl.activity.crosscondition;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.condition.ConditionUserData;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossConditionDaoImpl extends TempDao {


    public boolean addDataList(List<ConditionUserData> list) {
        boolean result = false;
        getDb().setConnName(DbNameStrategy.getCross2Db());
        String sql = "INSERT INTO t_c_activity_condition_notice(activityId,userId,conditionType,value,param,updateTime)" +
                " VALUES(?,?,?,?,?,?)";
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ConditionUserData data : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getConditionType()));
            para.put(i++, new DbParameter(Types.VARCHAR, data.getValue().toString()));
            para.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public boolean updateDataList(List<ConditionUserData> list) {
        boolean result = false;
        String sql = "UPDATE t_c_activity_condition_notice SET value=?,param=?,updateTime=?" +
                " WHERE activityId=? and userId = ? AND conditionType=?";
        getDb().setConnName(DbNameStrategy.getCross2Db());
        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        for (ConditionUserData data : list) {
            Map<Integer, DbParameter> para = new HashMap<>();
            int i = 1;
            para.put(i++, new DbParameter(Types.VARCHAR, data.getValue().toString()));
            para.put(i++, new DbParameter(Types.VARCHAR, data.getParam()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateTime()));

            para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getConditionType()));
            mapList.add(para);
        }
        if (getDb().executeBatchQuery(sql, mapList) > -1) {
            result = true;
        }
        return result;
    }

    public Map<Long,Map<Integer, ConditionUserData>> getDataMap(int activityId) {
        Map<Long,Map<Integer, ConditionUserData>> dataMap = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_c_activity_condition_notice WHERE activityId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ConditionUserData data = initData(rs);
                if(!dataMap.containsKey(data.getUserId())){
                    dataMap.put(data.getUserId(),new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUserId()).put(data.getConditionType(),data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private ConditionUserData initData(ResultSet rs) throws SQLException {
        ConditionUserData data = new ConditionUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setConditionType(rs.getInt("conditionType"));
        data.setValue(new BigInteger(rs.getString("value")));
        data.setParam(rs.getString("param"));
        data.setUpdateTime(rs.getLong("updateTime"));
        return data;
    }

}
